/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projeto.voztg.pkg2.pkg0.Listener;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 *
 * @author cris
 */
public class Dispatcher {

    private static Dispatcher instance;
    private Set<Listener> listeners;

    private Dispatcher() {
        this.listeners = new HashSet<>();
    }

    public static Dispatcher getInstance() {
        if (instance == null) {
            instance = new Dispatcher();
        }
        return instance;
    }

    public void addListener(Listener listener) {
        this.listeners.add(listener);
    }

    public void removeListener(Listener listener) {
        this.listeners.remove(listener);
    }

    public void dispatchEvent(Evento event) {
        Iterator<Listener> iterator = listeners.iterator();

        while (iterator.hasNext()) {
            Listener listener = (Listener) iterator.next();
            listener.receivedEvent(event);
        }
    }
}
